package cn.com.pxto.web.excel;

import java.io.OutputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;

import org.apache.struts.action.Action;

import cn.com.pxto.commons.StringUtils;
import cn.com.pxto.model.ExcelGuest;

public class CreatExcel extends Action {

	public void createUserExcel(List<ExcelGuest> listAll,
			HttpServletResponse response) {
		jxl.write.WritableWorkbook wwb = null;
		try {
			String fileName = StringUtils.long2DateString(new Date().getTime())
					+ ".xls";
			OutputStream os = response.getOutputStream();
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment; filename="
					+ fileName);
			wwb = Workbook.createWorkbook(os);
			jxl.write.WritableFont wfc = new jxl.write.WritableFont(
					WritableFont.COURIER, 18, WritableFont.BOLD, true);
			jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(
					wfc);
			wcfFC.setAlignment(jxl.format.Alignment.CENTRE);
			wcfFC.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

			jxl.write.WritableFont wfc1 = new jxl.write.WritableFont(
					WritableFont.COURIER, 10, WritableFont.BOLD, true);
			jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat(
					wfc1);
			wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);
			wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			wcfFC1.setWrap(true);

			jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat();
			wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);
			wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			wcfFC2.setWrap(true);
//			设置表名
			WritableSheet wsheet = wwb.createSheet("sheet1", 0);
//			合并出标题
			wsheet.mergeCells(0, 0, 6, 0);
//			设置小标题
		    wsheet.addCell(new Label(0,0,"学员名单",wcfFC));
		    wsheet.addCell(new Label(0,1,"时间",wcfFC1));
		    wsheet.addCell(new Label(1,1,"姓名",wcfFC1));
		    wsheet.addCell(new Label(2,1,"电话",wcfFC1));
		    wsheet.addCell(new Label(3,1,"学校",wcfFC1));
		    wsheet.addCell(new Label(4,1,"班级",wcfFC1));
		    wsheet.addCell(new Label(5,1,"分类",wcfFC1));
		    wsheet.addCell(new Label(6,1,"备注",wcfFC1));
//			开始填写内容
			Iterator<ExcelGuest> it = listAll.iterator();
			int i =1 ;
			while(it.hasNext()){
				ExcelGuest eg = it.next();
				wsheet.addCell(new Label(0, i+1, eg.getTime(), wcfFC2));
				wsheet.addCell(new Label(1, i+1, eg.getName(), wcfFC2));
				wsheet.addCell(new Label(2, i+1, eg.getTel(), wcfFC2));
				wsheet.addCell(new Label(3, i+1, eg.getSchoolName(), wcfFC2));
				wsheet.addCell(new Label(4, i+1, eg.getLessonName(), wcfFC2));
				wsheet.addCell(new Label(5, i+1, eg.getType(), wcfFC2));
				wsheet.addCell(new Label(6, i+1, eg.getRemark(), wcfFC2));
				i++;
			}
			wwb.write();
			wwb.close();
			os.close();
			System.out.println("Success Create Excel:" + fileName + ".xls");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
